package com.isunland.managesystem.ui;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import com.isunland.managesystem.R;
import com.isunland.managesystem.base.BaseVolleyActivity;
import com.isunland.managesystem.common.DynamicConfigLab;
import com.isunland.managesystem.entity.ProductSearchContent;

/**
 * Created by lenovo on 2016/6/15.
 */
public class ProductDevelopCountSearchFragment extends Fragment
    implements CompoundButton.OnCheckedChangeListener {
  protected static final String EXTRA_COUNT =
      "com.isunland.managesystem.ui.ProductDevelopCountSearchFragment.EXTRA_COUNT";

  private CheckBox cbMonth;
  private LinearLayout llMarket;
  private CheckBox cbMarket;
  private CheckBox cbCustomer;
  private CheckBox cbSalesman;
  //    private CheckBox cbProductType;
  //    private CheckBox cbProduct;

  private ProductSearchContent queryContent;
  private BaseVolleyActivity mActivity;

  @Override public void onCreate(Bundle savedInstanceState) {
    setHasOptionsMenu(true);
    if (NavUtils.getParentActivityName(getActivity()) != null) {
      ((BaseVolleyActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    ((BaseVolleyActivity) getActivity()).getSupportActionBar().setTitle(R.string.countSearch);
    mActivity = (BaseVolleyActivity) getActivity();
    queryContent = (ProductSearchContent) getArguments().getSerializable(EXTRA_COUNT);
    //        LogUtil.i("初始化统计项目==" + queryContent.toString());
    super.onCreate(savedInstanceState);
  }

  @Override public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    //        View v=inflater.inflate(R.layout.fragment_product_count,container,false);
    View v = inflater.inflate(R.layout.fragment_product_develop_count, container, false);

    findViews(v);

    //动态配置列   ---隐藏和显示，title
    DynamicConfigLab.newInstance(mActivity, getString(R.string.module_developExpenseEecord))
        .autoConfigTitle(v);
    DynamicConfigLab.newInstance(mActivity, getString(R.string.module_developExpenseEecord))
        .autoConfigVisiblity(v);

    initData();
    return v;
  }

  private void findViews(View v) {
    cbMonth = (CheckBox) v.findViewById(R.id.cb_month);
    llMarket = (LinearLayout) v.findViewById(R.id.ll_market);
    cbMarket = (CheckBox) v.findViewById(R.id.cb_market);
    cbCustomer = (CheckBox) v.findViewById(R.id.cb_customer);
    cbSalesman = (CheckBox) v.findViewById(R.id.cb_salesman);

    cbMonth.setOnCheckedChangeListener(this);
    cbMarket.setOnCheckedChangeListener(this);
    cbCustomer.setOnCheckedChangeListener(this);
    cbSalesman.setOnCheckedChangeListener(this);
  }

  private void initData() {
    if (!TextUtils.isEmpty(queryContent.getMonth())) {
      cbMonth.setChecked(true);
    } else {
      cbMonth.setChecked(false);
    }
    if (!TextUtils.isEmpty(queryContent.getMarket())) {
      cbMarket.setChecked(true);
    } else {
      cbMarket.setChecked(false);
    }
    if (!TextUtils.isEmpty(queryContent.getSaleman())) {
      cbSalesman.setChecked(true);
    } else {
      cbSalesman.setChecked(false);
    }
    if (!TextUtils.isEmpty(queryContent.getCustomer())) {
      cbCustomer.setChecked(true);
    } else {
      cbCustomer.setChecked(false);
    }
    //        if (!TextUtils.isEmpty(queryContent.getProductType())){
    //            cbProductType.setChecked(true);
    //        }else {
    //            cbProductType.setChecked(false);
    //        }

  }

  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_confirm, menu);
    super.onCreateOptionsMenu(menu, inflater);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      // 响应返回图标
      case android.R.id.home:

        if (NavUtils.getParentActivityName(getActivity()) != null) {
          NavUtils.navigateUpFromSameTask(getActivity());
        }
        break;

      case R.id.menu_item_confirm:
        queryContent.setProduct(ProductSearchContent.PRODUCT);
        queryContent.setProductType(ProductSearchContent.PRODUCT_TYPE);
        if (llMarket.getVisibility() == View.GONE) {
          queryContent.setMarket("");
        }
        Intent i = new Intent();
        i.putExtra(EXTRA_COUNT, queryContent);
        getActivity().setResult(Activity.RESULT_OK, i);
        getActivity().finish();

        break;

      default:
        break;
    }
    return super.onOptionsItemSelected(item);
  }

  @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    switch (buttonView.getId()) {
      case R.id.cb_month:
        if (isChecked) {
          queryContent.setMonth(ProductSearchContent.MONTH);
        } else {
          queryContent.setMonth("");
        }
        break;
      case R.id.cb_market:
        if (isChecked) {
          queryContent.setMarket(ProductSearchContent.MARKET);
        } else {
          queryContent.setMarket("");
        }
        break;
      case R.id.cb_customer:
        if (isChecked) {
          queryContent.setCustomer(ProductSearchContent.CUSTOMER);
        } else {
          queryContent.setCustomer("");
        }
        break;
      case R.id.cb_salesman:
        if (isChecked) {
          queryContent.setSaleman(ProductSearchContent.SALEMAN);
        } else {
          queryContent.setSaleman("");
        }
        break;
      //            case R.id.cb_productType:
      //                if (isChecked){
      //                    queryContent.setProductType(ProductSearchContent.PRODUCT_TYPE);
      //                }else {
      //                    queryContent.setProductType("");
      //                }
      //            case R.id.cb_product:
      //                if (isChecked){
      //                    queryContent.setProduct(ProductSearchContent.PRODUCT);
      //                }else {
      //                    queryContent.setProduct("");
      //                }
      //                break;

      default:
        break;
    }
  }

  protected static ProductDevelopCountSearchFragment newInstence(ProductSearchContent content) {
    Bundle bundle = new Bundle();
    bundle.putSerializable(EXTRA_COUNT, content);
    ProductDevelopCountSearchFragment mfragment = new ProductDevelopCountSearchFragment();
    mfragment.setArguments(bundle);
    return mfragment;
  }
}
